#coding=cp936
import os
import sys
import shutil

def read_all_file(dirname,filname):
    exts=['.pas','.dfm']

    if os.path.isfile(dirname):
        exta=os.path.splitext(dirname)[1]
        if exta.lower() in exts:
            readhandle = open(dirname,'r')
            #writhandle.write('<----\n')
            #writhandle.write(dirname+'\n')
            alllines = readhandle.readlines()
            writhandle = open(dirname,'w')
            for eachline in alllines:
                finded = eachline.find('GetLocalUnit')
                #print (finded)
                if finded != -1 :
                    print('%s in %s in %s' % (finded,eachline,dirname))
                    #print(eachline.replace('TADOConnection','TUniConnection'))
                    eachline = eachline.replace('GetLocalUnit','GetRealUnit')
                writhandle.write(eachline)
                #print(eachline.strip())
                #print(eachline.find('TADOConnection'))
                #writhandle.write(eachline)
            #writhandle.write('----->\n')
            writhandle.close()
            readhandle.close()

            
                
    elif os.path.isdir(dirname):
        for item in os.listdir(dirname):
            itempath = os.path.join(dirname,item)
            read_all_file(itempath,filname)

if __name__ == "__main__":
    dirname = r'D:\GITOSC\frDemo\2015_05_29_11_13_43_D7_Example00001'
    filname = r'E:\total.txt'

    truepassword = r'go'
    temppassword = raw_input('enter password:')
    #print temppassword
    if truepassword != temppassword :
        sys.exit()
    print(r'successful')   
    
    read_all_file(dirname,filname)  
            
        

